import {Restivus} from 'meteor/nimble:restivus';
import {Meteor} from 'meteor/meteor';
import { Random } from 'meteor/random';
import {AnFangs} from '../imports/api/an_fangs';
import {Gateways} from '../imports/api/gateways';

if (Meteor.isServer) {
    Meteor.startup(() => {
        let Api = new Restivus({
            useDefaultAuth: true,
            prettyJson: true,
        });
       
        //修改安防信息
        Api.addRoute('updateAnFang', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const familyId = param.familyId;
                        const devices = JSON.parse(param.devices).devices;
                        const name = param.name;
                        console.log('devices : ', devices);
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        if(user) {
                            /*AnFangs.insert({name: '外出布防', familyId: familyId, status: false, devices: []});
                            AnFangs.insert({name: '在家布防', familyId: familyId, status: false, devices: []});
                            AnFangs.insert({name: '撤防', familyId: familyId, status: false, devices: []});*/
                            const af = AnFangs.update({familyId: familyId, name: name}, {$set: {devices: devices}});
                            console.log('familyId : ', familyId);
                            console.log('name : ', name);
                            console.log('af : ', af);
                            if(af){
                                return {success: true, result: '安防信息修改成功！', resultCode: 1000}
                            }else{
                                return {success: false, result: '安防信息修改失败！', resultCode: 1002}
                            }
                        }else{
                            return {success: false, result: '请登录！', resultCode: 1004};
                        }
                    }
                    return {success: false, result: '参数有误！', resultCode: 1002};
                }
            }
        });
        //触发安防
        Api.addRoute('triggerAnFang', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const id = param.id;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        console.log('id : ', id);
                        if(user) {
                            const af = AnFangs.findOne({_id: id});
                            const uf = AnFangs.update({_id: id}, {$set: {status: !af.status}});
                            if(uf){
                                const anFangs = AnFangs.find({familyId: af.familyId}).fetch();
                                if(!af.status){
                                    anFangs.forEach((a) => {
                                        if(a._id != id){
                                            AnFangs.update({_id: a._id}, {$set: {status: false}});
                                        }
                                    })
                                }
                                return {success: true, result: '触发成功！', resultCode: 1000}
                            }else{
                                return {success: false, result: '触发失败！', resultCode: 1002}
                            }
                        }else{
                            return {success: false, result: '请登录！', resultCode: 1004};
                        }
                    }
                    return {success: false, result: '参数有误！', resultCode: 1002};
                }
            }
        });
        //查询安防信息
        Api.addRoute('getAnFang', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const familyId = param.familyId;
                        const name = param.name;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        if(user) {
                            // AnFangs.insert({name: '外出布防', familyId: familyId, status: false, devices: [], createdName: user.username});
                            // AnFangs.insert({name: '在家布防', familyId: familyId, status: false, devices: [], createdName: user.username});
                            // AnFangs.insert({name: '撤防', familyId: familyId, status: false, devices: [], createdName: user.username});
                            const af = AnFangs.findOne({familyId: familyId, name: name});
                            if(af){
                                return {success: true, result: {anFang: af, error: '安防信息查询成功！'}, resultCode: 1000}
                            }else{
                                return {success: false, result: {error: '安防信息查询失败！'}, resultCode: 1002}
                            }
                        }else{
                            return {success: false, result: {error: '请登录！'}, resultCode: 1004};
                        }
                    }
                    return {success: false, result: {error: '参数有误！'}, resultCode: 1002};
                }
            }
        });
        //根据家庭ID查询安防信息
        Api.addRoute('getAnFangByFamily', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const familyId = param.familyId;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        if(user) {
                            const afs = AnFangs.find({familyId: familyId}).fetch();
                            if(afs){
                                return {success: true, result: {anFangs: afs, error: '安防信息查询成功！'}, resultCode: 1000}
                            }else{
                                return {success: false, result: {error: '安防信息查询失败！'}, resultCode: 1002}
                            }
                        }else{
                            return {success: false, result: {error: '请登录！'}, resultCode: 1004};
                        }
                    }
                    return {success: false, result: {error: '参数有误！'}, resultCode: 1002};
                }
            }
        });
        //根据家庭Id及安防name和设备ID删除传感器
        Api.addRoute('deleteSonserByAnFang', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const familyId = param.familyId;
                        const name = param.name;
                        const deviceId = param.deviceId;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        if(user) {
                            const af = AnFangs.findOne({familyId: familyId, name: name});
                            let devices = af.devices;
                            devices = devices.filter((x) => x._id !== deviceId);
                            const uf = AnFangs.update({_id: af._id}, {$set: {devices: devices}});
                            if(uf){
                                return {success: true, result: '传感器删除成功！', resultCode: 1000}
                            }else{
                                return {success: false, result: '传感器修改失败！', resultCode: 1002}
                            }
                        }else{
                            return {success: false, result:  '请登录！', resultCode: 1004};
                        }
                    }
                    return {success: false, result: '参数有误！', resultCode: 1002};
                }
            }
        });
        //查询安防中不存在的传感器
        Api.addRoute('selectSensorNotInAnFang', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const familyId = param.familyId;
                        const name = param.name;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        if(user) {
                            const gws = Gateways.find({'familyId': familyId}).fetch();
                            let devices = [];
                            gws.forEach((gw) => {
                                devices = devices.concat(gw.devices);
                            })
                            devices = devices.filter((x) => x.isSensor == true);  //选择出传感器

                            const af = AnFangs.findOne({familyId: familyId, name: name});
                            let arr = [];
                            if(af){
                                let devs = af.devices || [];
                                devs.forEach((dev) => {
                                    arr.push(dev._id);
                                });
                            }
                            devices = devices.filter((x) => arr.indexOf(x.d_id) == -1);
                            if(devices){
                                return {success: true, result: {devices: devices, error: '查询成功！'}, resultCode: 1000}
                            }else{
                                return {success: false, result: {error: '查询失败！'}, resultCode: 1002}
                            }
                        }else{
                            return {success: false, result:  {error: '请登录！'}, resultCode: 1004};
                        }
                    }
                    return {success: false, result: {error: '参数有误！'}, resultCode: 1002};
                }
            }
        });
    });

} 